package edu.arizona.sista.learn.model.holding;

import java.io.File;
import java.util.List;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;

import edu.arizona.sista.learn.util.Utils;
import edu.arizona.sista.learn.util.Utils.MovieName;

public class Main {
	
	public static void main(String args[]) throws Exception {
		
		JSAP jsap = new JSAP();
		Switch helpSwitch = new Switch("help")
				.setShortFlag('h')
				.setLongFlag("help");
		FlaggedOption pose = new FlaggedOption("pose-file")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("pose-file");
		FlaggedOption bodyParts = new FlaggedOption("bodyParts-file")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("bodyParts-file");
		FlaggedOption output = new FlaggedOption("output-file")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("output-file");

		jsap.registerParameter(helpSwitch);
		jsap.registerParameter(pose);
		jsap.registerParameter(bodyParts);
		jsap.registerParameter(output);

		JSAPResult config = jsap.parse(args);
		if (config.getBoolean("help") || "".equals(config.getString("pose-file")) || "".equals(config.getString("bodyParts-file")) || "".equals(config.getString("output-file"))) {
			//System.out.print("java -jar holding.jar ");
			System.out.println(jsap.getUsage());
			return;
		}
		File poseFile = new File(config.getString("pose-file"));
		if (!poseFile.isFile()) {
			System.err.println("File not found or invalid!");
			return;
		}
		File bodyPartsFile = new File(config.getString("bodyParts-file"));
		if (!bodyPartsFile.isFile()) {
			System.err.println("File not found or invalid!");
			return;
		}
		
		holdObjects(poseFile, bodyPartsFile, config.getString("output-file"));

	}

	private static void holdObjects(File poseFile, File bodyPartsFile, String output) {
		
		//Name of the scene
		MovieName sceneName = MovieName.Map;
		if (poseFile.getAbsolutePath().toLowerCase().contains("newspaper")) sceneName = MovieName.Newspaper;
		
		//Load data
		Scene scene = new Scene(sceneName);
		scene.loadPositionData(poseFile);
		scene.loadBodyParts(bodyPartsFile);

		List<HoldingAnObject> obama = HoldingAnObject.getHoldThings(scene);
		
		StringBuilder sb = new StringBuilder();
		for (HoldingAnObject barak : obama) sb.append(barak);
		Utils.toFile(sb, output);
		
		//System.out.println(sb.toString());
	}
}
